﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using BaseDll;
using UIDATALayer;
namespace UserData
{
    public class TestTimeData : ShowData
    {
        public string strName { get; set; }
    }

    public class TestTime_数据统计
    {
        public int n_Total = 0;
        public int n_OK = 0;
        public double d_CTAllTime = 0;
        public double d_CTAllCount = 0;
        public double d_LimitTime = double.MaxValue;
    }
    public class 数据统计_Save
    {
        public TestInfo time_当前时间 { get; set; } = new TestInfo { Name = "当前时间", Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Max = " ", Min = " " };
        public TestInfo str_Name { get; set; } = new TestInfo { Name = "名称", Value = "", Max = " ", Min = " " };
        public TestInfo n_Total { get; set; } = new TestInfo { Name = "产品总量", Value = "", Max = " ", Min = " " };
        public TestInfo n_OK { get; set; } = new TestInfo { Name = "OK", Value = "", Max = " ", Min = " " };
        public TestInfo n_NG { get; set; } = new TestInfo { Name = "NG", Value = "", Max = " ", Min = " " };
        public TestInfo 良率 { get; set; } = new TestInfo { Name = "良率", Value = "", Max = " ", Min = " " };
        public TestInfo UPH { get; set; } = new TestInfo { Name = "UPH", Value = "", Max = " ", Min = " " };

    }
    public class TestTimeMgr
    {
        private static TestTimeMgr testTimeMgr = null;
        private static object obj = new object();
        string str_PathName = @"D:\HY System\_Machine\Config\设备运行数据统计.json";
        public Dictionary<string, TestTime_数据统计> dic_TestTime = new Dictionary<string, TestTime_数据统计>();
        public delegate void ReflushEventHandle(string strName);
        public static ReflushEventHandle EvenReflushTestData = null;
        public static TestTimeMgr GetInstance()
        {
            if (testTimeMgr == null)
            {
                lock (obj)
                {
                    testTimeMgr = new TestTimeMgr();
                    return testTimeMgr;
                }

            }
            else
                return testTimeMgr;
        }
        public void Init()
        {
            if (!File.Exists(str_PathName))
            {
                AccessJosnSerializer.ObjectToJson(str_PathName, dic_TestTime);
            }
            else
            {
                object obj = AccessJosnSerializer.JsonToObject(str_PathName, typeof(Dictionary<string, TestTime_数据统计>));
                if (obj != null)
                {
                    dic_TestTime = (Dictionary<string, TestTime_数据统计>)obj;
                }
            }
            foreach (var item in dic_TestTime)
            {
                if (EvenReflushTestData != null)
                    EvenReflushTestData(item.Key);
            }
        }
        public void Function_ClearData()
        {

            string str_PathNameSave = @"D:\设备生产数据备份\生产数据统计.csv";
            if (!Directory.Exists(Directory.GetParent(str_PathNameSave).FullName))
            {
                Directory.CreateDirectory(Directory.GetParent(str_PathNameSave).FullName);
            }

            List<string> listKeys= dic_TestTime.Keys.ToList();

            foreach (var item in listKeys)
            {
                数据统计_Save info = new 数据统计_Save();
                info.time_当前时间.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                info.str_Name.Value = item;
                info.n_Total.Value = dic_TestTime[item].n_Total;
                info.n_OK.Value = dic_TestTime[item].n_OK;
                info.n_NG.Value = dic_TestTime[item].n_Total - dic_TestTime[item].n_OK;
                info.良率.Value = (((double)dic_TestTime[item].n_OK / (double)dic_TestTime[item].n_Total) * 100).ToString("0.00") + "%"; ;
                info.UPH.Value = (3600.0 * dic_TestTime[item].d_CTAllCount / dic_TestTime[item].d_CTAllTime).ToString("0.00");
                CSVHelper.Instance.SaveToCSVPath(str_PathNameSave, info);
                dic_TestTime[item] = new TestTime_数据统计();
                if (EvenReflushTestData != null)
                    EvenReflushTestData(item);

            }
            AccessJosnSerializer.ObjectToJson(str_PathName, dic_TestTime);
        }
        public void Function_AddTestTime(string strName, double n_用时时间, bool b_Result)
        {
            if(!dic_TestTime.ContainsKey(strName))
            {
                dic_TestTime.Add(strName, new TestTime_数据统计());
            }
            if (b_Result)
            {
                dic_TestTime[strName].n_Total++;
                dic_TestTime[strName].n_OK++;
                if (n_用时时间 >0)
                {
                    dic_TestTime[strName].d_CTAllCount++;
                    dic_TestTime[strName].d_CTAllTime = dic_TestTime[strName].d_CTAllTime + n_用时时间;
                }
            }
            else
            {
                dic_TestTime[strName].n_Total++;
            }
            if (EvenReflushTestData != null)
                EvenReflushTestData(strName);
            AccessJosnSerializer.ObjectToJson(str_PathName, dic_TestTime);
        }
    }
}
